import { _decorator, Button, Component, HtmlTextParser, Label, Node, tween, v2, v3 } from 'cc';
import { UIBase } from './UIBase';
import { GameData } from './GameData';
const { ccclass, property } = _decorator;

@ccclass('Settlement')
export class Settlement extends UIBase {
    private addGoldL:Label=null;
    private goldNumL:Label=null;
    //private continueBtn:Button=null;
    oninit(...rest: any[]): void {
        this.node.parent.getChildByName("JokerMgr").setSiblingIndex(100);
        this.addGoldL=this.getComp<Label>("_AddGoldLabel","Label");
        this.goldNumL=this.getComp<Label>("_GoldnumL","Label");
        //this.continueBtn=this.getComp<Button>("_ContinueBtn","Button");
        this.addClickEventL("_ContinueBtn",()=>{
            this.closeUI("Settlement",true);
            this.openUI("Shop");
        })
        this.addGoldL.string="";
        this.goldNumL.string="";
        //将继续按钮隐藏打完字在显示
        this.getNode("_ContinueBtn").active=false;
        this.settlement();
        

    }
    //结算 
    settlement(){
        //打字机
        let addString1="剩余出牌次数+";
        let addString2="利息+";
        //出牌次数        
        let playCardnum=GameData.playCardnum;
        if(playCardnum>0){
            GameData.coin+=playCardnum;
        }
        addString1+=playCardnum.toString();
        //利息
        let interest=Math.floor(GameData.coin/5);
        GameData.coin+=interest;
        addString2+=interest.toString();
        //打字
        addString1+="\n"+addString2+"\n";
        this.typeWord(addString1,0);
        this.goldNumL.string="总计:"+GameData.coin.toString();
    }
    typeWord(word:string,index:number){
        this.addGoldL.string+=word[index];
        let isnum=false;
        //将数字放大
        if(word[index+1]=="\n"){
            isnum=true;
            tween(this.addGoldL.node)
            .to(0.2,{scale:v3(1.5,1.5,1)})
            .to(0.2,{scale:v3(1,1,1)})
            .start();
        }
        index++;
        //console.log(this.addGoldL.string);
        if (index >= word.length) {
            //打字结束将继续按钮显示
            this.getNode("_ContinueBtn").active=true;
            return;
        }
        if(isnum){
            setTimeout(() => {
                this.typeWord(word, index);
            }, 400);
        }
        else (
            setTimeout(() => {
                this.typeWord(word, index);
            }, 100)
        )
    }
}


